from flask import Flask

from flask_sqlalchemy import SQLAlchemy
from flask_session import Session
#导入redis
from redis import StrictRedis
#导入flask_wtf扩展实现项目的csrf保护
from flask_wtf import CsrfProtect,csrf
#导入config
from config import config,Config
#实例化SQLAlchemy对象
db = SQLAlchemy()
#实例化redis对象用来存储和业务逻辑相关的数据比如验证码图片
redis_store = StrictRedis(host=Config.REDIS_HOST,port=Config.REDIS_PORT,decode_responses=True)

from config import config
def return_app(config_name):
    app = Flask(__name__)
    app.config['SESSION_TYPE'] = 'filesystem'
#/home/python/info3/info/__init__.py
    #使用config
    app.config.from_object(config[config_name])
    #通过init方法让db和app进行关联
    db.init_app(app)
    #实例化Session
    Session(app)
    @app.after_request
    def after_request(response):
        csrf_token = csrf.generate_csrf()
        response.set_cookie('csrf_token',csrf_token)
        return response
    #注册蓝图
    from info.modules.news import news_blue
    app.register_blueprint(news_blue)
    #注册蓝图
    from info.modules.passport import passport_blue
    #使用蓝图
    app.register_blueprint(passport_blue)
    return app